import {
  addressMas,
  deleteadd
} from '../../../../api.js'
var app = getApp(); //获取App内部的信息
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lis: [],
    eventPrevent:false,
    addChoice:1,
    is_iPhoneX:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this
    console.log(app.globalData.is_iPhoneX)
    if (app.globalData.is_iPhoneX){
      this.setData({
        is_iPhoneX:true
      })
    }
    console.log(options)
   
    if (options.addChoice == 0){
      that.setData({
        eventPrevent:false,
        addChoice: options.addChoice
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    addressMas({
      token: app.globalData.token,
    }).then(res => {
      console.log(res)
      if (res.data.code == 666) {
        that.setData({
          lis: res.data.data
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
    this.onLoad(this.data.addChoice)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
  new_add_btn: function() {
    var num = this.data.addChoice
    wx.redirectTo({
      url: '/pages/user/setUp/myadd/newlyadded/newlyadded?mold=0&addChoice=' + num,
    })
  },
  adds_btn_del: function(e) {
    var ids = e.currentTarget.dataset.id;
    var indexs = e.currentTarget.dataset.index;
    var that = this;
    wx.showModal({
      title: '删除',
      content: '是否确认删除',
      confirmColor: '#000',
      success: function(e) {
        console.log(e)
        if (e.confirm) {
          deleteadd({
            token: app.globalData.token,
            id: ids
          }).then(res => {
            console.log(res)
            if (res.data.code == 666) {
              wx.showToast({
                title: '删除成功',
              })
              var node = that.data.lis;
              node.splice(indexs, 1)
              that.setData({
                lis: node
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  adds_btn_edit: function(e) {
    var indexs = e.currentTarget.dataset.index;
    var aut = this.data.lis[indexs];
    var addChoice = this.data.addChoice
    wx.navigateTo({
      url: '/pages/user/setUp/myadd/newlyadded/newlyadded?mold=1&toAuth=' + JSON.stringify(aut) + '&ind=' + addChoice,
    })
  },
  pickAddress:function(e){
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    var num = e.currentTarget.dataset.index;
    var that = this
    prevPage.setData({
      defaultadd: that.data.lis[num]
    })
    wx.navigateBack({
      delta: 1
    })
  }
})